<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前定位城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button location">
              <!-- <location class="autolocation"></location> -->
              {{this.currentCity1}}
            </div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div
            class="button-wrapper"
            v-for="item in hot"
            :key="item.id"
            @click="handleCityClick(item.name)"
          >
            <div class="button">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item,key) in cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <div class="item-list">
          <div
            class="item border-bottom"
            v-for="innerItem in item"
            :key="innerItem.id"
            @click="handleCityClick(innerItem.name)"
          >{{innerItem.name}}</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import BScroll from "better-scroll";
//导入定位组件
import location from "../../subcomponents/map.vue";
import { mapState, mapMutations } from "vuex";
export default {
  name: "CityList",
  computed: {
    ...mapState({
      currentCity: "city"
    }),
    ...mapState({
      currentCity1: "city1"
    })
  },
  activated() {
    this.scroll = new BScroll(this.$refs.wrapper, { click: true });
  },
  methods: {
    handleCityClick(city) {
      //console.log(city);
      //this.$store.commit("changeCity", city);
      this.$router.push("/home");
      this.$store.commit("changeCity", city);
      //this.$router.push("/store");
      //console.log(this.$route.path);
      //console.log(this.$router);
      // if (this.$route.path === "/home/location") {
      //   this.$router.replace("/home");
      //   //this.$route.path = "";
      // } else {
      //   this.$router.push("/store");
      // }
    }
    //...mapMutations(["changeCity"])
  },
  props: ["hot", "cities", "letter"],
  watch: {
    // letter : function(newVal){
    //   const element = this.$refs[newVal];
    //   this.scroll.scrollToElement(element);
    // }
    letter() {
      if (this.letter) {
        const element = this.$refs[this.letter][0];
        // console.log(element);
        // console.log(this);
        //console.log([this.letter]);
        //console.log([this.letter][0]);
        this.scroll.scrollToElement(element);
      }
    }
  },
  components: {
    location
  }
};
</script>
<style lang="scss" scoped>
.border-topbottom {
  &:before {
    border-color: #ccc;
  }

  &:after {
    border-color: #ccc;
  }
}

.border-bottom {
  &:before {
    border-color: #ccc;
  }
}

.list {
  overflow: hidden;
  position: absolute;
  top: 100px;
  left: 0;
  right: 0;
  bottom: 0;

  .title {
    line-height: 20px;
    background: #eee;
    left: 5%;
    position: relative;
    color: black;
    font-size: 15px;
  }

  .button-list {
    box-sizing: border-box;
    // padding: 10px;
    overflow: hidden;
    position: relative;
    left: 5%;
    width: 90%;

    .button-wrapper {
      float: left;
      width: 33.33%;
      padding: 5px;

      .button,
      .autolocation {
        // margin: 5px;
        // margin-right: 5px;
        padding: 5px 0;
        text-align: center;
        background-color: #fff;
        border-radius: 5px;
        font-size: 15px;
      }
      .location {
        color: red;
      }
    }
  }

  .item-list {
    background-color: #fff;
  }

  .item {
    line-height: 40px;
    width: 90%;
    left: 5%;
    position: relative;
  }
}
</style>